import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Super2 implements Serializable {
	private static final long serialVersionUID = 1L;
	int d;
}
class Super extends Super2 implements Serializable {
	private static final long serialVersionUID = 1L;
	int a;
	public Super(int a) {
		this.a = a;
	}
}
class Sub extends Super implements Externalizable {
	private static final long serialVersionUID = 1L;
	int b, c;
	public Sub() {
		super(9);
	}
	public Sub(int b, int c) {
		super(5);
		this.b = c;
		this.c = b;
	}
	public void writeExternal(ObjectOutput out) throws IOException {
		out.write(b);
		out.write(c);
	}
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		b = in.read();
		c = in.read();
	}
}
public class SerializationTest {
	private static final String FILE = "C:/Users/Naveen/Downloads/Test.ser";
	private static void serializeToFile() throws IOException {
		Sub sub = new Sub(2, 3);
		sub.a = 1;
		sub.d = 4;
		FileOutputStream out = new FileOutputStream(FILE);
		ObjectOutputStream objStream = new ObjectOutputStream(out);
		objStream.writeObject(sub);
		objStream.flush();
		out.flush();
		objStream.close();
		out.close();
	}
	private static void deserializeFromFile() throws IOException, ClassNotFoundException {
		FileInputStream in = new FileInputStream(FILE);
		ObjectInputStream objInStream = new ObjectInputStream(in);
		Sub sub = (Sub)objInStream.readObject();
		objInStream.close();
		System.out.println(sub.a);
		System.out.println(sub.b);
		System.out.println(sub.c);
		System.out.println(sub.d);
	}
	/**
	 * @param args
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		serializeToFile();
		deserializeFromFile();
	}
}
